package com.yzqp.generator.prop.java;

import com.yzqp.generator.util.FileUtil;
import com.yzqp.generator.util.HumpNameUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * 生成的包的属性
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "generator.bean")
@PropertySource("classpath:application-generator.yml")
public class GeneratorBeanProp {

    @Autowired
    public GeneratorBaseProp generatorBaseProp;

    /**
     * 最后的包名 bean dao service
     */
    private String lastPackage = "bean";

    /**
     * 最后的包名 bean dao service
     */
    private String postfix = "Bean";

    /**
     * 模板的包路径
     */
    private String templePath = "com.yzqp.generator.temple.java.entity.java_bean.txt";

    /**
     * 获得生成的文件路径
     * 即 返回 E:\\myproject\\blog\\blog_server\\src\\main\\java\\com\\yzqp\\generator\\test\\bean\\user.java
     *
     * @return
     */
    public String getFileName() {
        String className = HumpNameUtil.getJavaClassName(generatorBaseProp.getTableName(), this.getPostfix());
        return className;
    }

    /**
     * 获得生成的文件路径
     * 即 返回 E:\\myproject\\blog\\blog_server\\src\\main\\java\\com\\yzqp\\generator\\test\\bean\\user.java
     *
     * @return
     */
    public String getGeneratorFilePath() {
        String savePath = generatorBaseProp.getSavePath();
        savePath = savePath + File.separator + this.lastPackage + File.separator + this.getFileName() + ".java";
        return savePath;
    }

    /**
     * 获得 bean 路径
     * 即 返回 E:\\myproject\\blog\\blog_server\\src\\main\\java\\com\\yzqp\\generator\\test\\bean\\user.java
     *
     * @return
     */
    public String getTemplePath() {
        // 获得java 包的基础路径
        String baseJavaPath = FileUtil.getCurrentProjectJavaPackageRootPath();
        // 基础包路径
        String baseJavaPackage = this.templePath;
        // 通过 . 分割包路径
        String[] packageArr = baseJavaPackage.split("\\.");
        packageArr[packageArr.length - 2] = packageArr[packageArr.length - 2] + "." + packageArr[packageArr.length - 1];
        packageArr[packageArr.length - 1] = "";
        for (String packageItem : packageArr) {
            if (!StringUtils.isEmpty(packageItem)) {
                baseJavaPath = baseJavaPath + File.separator + packageItem;
            }
        }
        return baseJavaPath;
    }


    /**
     * 获得 bean 所在的包
     * 即 返回 com.yzqp.generator.test.bean
     *
     * @return
     */
    public String getBeanLocationPackage() {
        return generatorBaseProp.getBaseJavaPackage() + "." + this.lastPackage;
    }
}
